package com.rc.evcharger.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

import static com.rc.evcharger.common.utils.DataUtil.SPLIT;

/**
 * Create by Yang on 2021/10/30
 */
public class ToFrameUtil {

    public static String accountToFrame(BigDecimal account) {
        String s = String.valueOf(account.setScale(2, RoundingMode.CEILING.HALF_UP));
        String substring = s.replace(".", "");

        //补0
        String format = String.format("%08d", Integer.valueOf(substring));

        String accountReport = "";
        for (int i = 0; i < 4; i++) {
            accountReport = format.substring(i * 2, i * 2 + 2) + " " + accountReport;
        }
        return accountReport.trim();
    }

    /**
     * 瑞晨 爱普拉 订单号解析 30位
     *
     * @param orderNum
     * @return
     */
    public static String orderNumToFrame(String orderNum) {
        char[] c = orderNum.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < c.length; i = i + 2) {
            sb.append(c[i]).append(c[i + 1]).append(SPLIT);
        }
        return sb.toString().trim();
    }

    /**
     * 只做反转字符串操作、不进行数据转换
     *
     * @param orderNum
     * @return
     */
    public static String reverseToFrame(String value) {
        StringBuffer stringBuffer = new StringBuffer();
        //将数据进行低位在前高位在后    1234 3421
        for (int i = 0; i <= value.length() - 1; i = i + 2) {
            stringBuffer.append(value.substring(value.length() - 2 - i, value.length() - i)).append(SPLIT);
        }
        return stringBuffer.toString().trim();
    }


    /**
     * 金额转报文 8.54 <->54 08 00 00
     *
     * @param account
     * @return
     */
    public String parseAccount(BigDecimal account) {
        String s = String.valueOf(account.setScale(2, RoundingMode.CEILING.HALF_UP));
        String substring = s.replace(".", "");

        //补0
        String format = String.format("%08d", Integer.valueOf(substring));

        String accountReport = "";
        for (int i = 0; i < 4; i++) {
            accountReport = format.substring(i * 2, i * 2 + 2) + " " + accountReport;
        }
        String trim = accountReport.trim();

        return trim;
    }


    public static void main(String[] args) {
        System.out.println(ToFrameUtil.accountToFrame(new BigDecimal("1")));
        String s = DataUtil.toHexFrameByAccount(new BigDecimal("2.85"), 2);
        System.out.println(s);
        String value = "12345678";
        StringBuffer stringBuffer = new StringBuffer();
        //System.out.println(ToFrameUtil.reverseToFrame("12345678"));
        //将数据进行低位在前高位在后    12343421
    }
}
